﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TheHeGeo.Util;
using TheHeGeoDao;

namespace TheHeGeo.UserControl
{
    public partial class UcGrlTestData : System.Web.UI.UserControl
    {
        public string MaSensor { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               // Session[SessionConst.SES_FLAG_DATA_FOR_WEBCHAR + MaSensor] = null;
                //LoadTestData();
            }
            else
            {
                grlDataTest0.DataSource = Session[SessionConst.SES_GRL_DATA_TEST + MaSensor];
            }
        }

        public void LoadTestData()
        {
            grlDataTest0.GridView.KeyFieldName = "Id";
            //var dt = DaoHelper.Entities.DuLieuTests.Where(p => p.MaSensor == MaSensor);
            var dtTable = new DataTable();
            dtTable.Columns.Add("Id",typeof(int));
            dtTable.Columns.Add("MaSensor",typeof(string));
            dtTable.Columns.Add("ThoiGianBatDauTest",typeof(DateTime));
            dtTable.Columns.Add("ThoiGianKetThucTest",typeof(DateTime));
            dtTable.Columns.Add("GhiChuTest",typeof(string));
            var dr0 = dtTable.NewRow();
            dr0["Id"] = -1;
            dr0["MaSensor"] = MaSensor;
            dr0["ThoiGianBatDauTest"] = DBNull.Value;
            dr0["ThoiGianKetThucTest"] = DBNull.Value;
            dr0["GhiChuTest"] = "100 dữ liệu mới nhất";
            dtTable.Rows.Add(dr0);
           /* foreach (var d in dt)
            {
                var dr = dtTable.NewRow();
                dr["Id"] = d.Id;
                dr["MaSensor"] = d.MaSensor;
                if (d.ThoiGianBatDauTest.HasValue)
                    dr["ThoiGianBatDauTest"] = d.ThoiGianBatDauTest.Value;
                else
                {
                   dr["ThoiGianBatDauTest"]=DBNull.Value;
                }

                if (d.ThoiGianKetThucTest.HasValue)
                    dr["ThoiGianKetThucTest"] = d.ThoiGianKetThucTest;
                else
                {
                    dr["ThoiGianKetThucTest"] = DBNull.Value;
                }
                dr["GhiChuTest"] = d.GhiChuTest;
                dtTable.Rows.Add(dr);
            }*/
            Session[SessionConst.SES_FLAG_DATA_FOR_WEBCHAR + MaSensor] = new object[] { MaSensor, null, null };
            Session[SessionConst.SES_GRL_DATA_TEST + MaSensor] = dtTable;
            grlDataTest0.DataSource = dtTable;
            grlDataTest0.DataBind();
            grlDataTest0.ValueChanged -= grlDataTest_ValueChanged;
            if (Session[SessionConst.SES_ID_TEST_DATA + MaSensor] == null) 
                grlDataTest0.Value = -1;
            else
            {
                grlDataTest0.Value = (int)Session[SessionConst.SES_ID_TEST_DATA + MaSensor];
            }
            grlDataTest0.ValueChanged += grlDataTest_ValueChanged;
            grlDataTest0.Visible = false;
        }

        protected void grlDataTest_ValueChanged(object sender, EventArgs e)
        {
            var fromDate = grlDataTest0.GridView.GetRowValuesByKeyValue(grlDataTest0.Value, "ThoiGianBatDauTest");
            var toDate = grlDataTest0.GridView.GetRowValuesByKeyValue(grlDataTest0.Value, "ThoiGianKetThucTest");
            Session[SessionConst.SES_FLAG_DATA_FOR_WEBCHAR + MaSensor]=new object[]{MaSensor,fromDate,toDate};
            Session[SessionConst.SES_ID_TEST_DATA + MaSensor] = grlDataTest0.Value;
            Response.Redirect(Request.Url.ToString());
        }
    }
}